package com.csm.admin.config;

import com.csm.admin.interceptor.LoginInterceptor;
import com.csm.admin.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器中（实现WebMvcConfigurer的addInterceptors方法）
 * 3、指定拦截器规则【如果拦截所有，静态资源也会被拦截】
 * @author csm
 */

@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
    /**
     * 从容器中拿到 RedisUrlCountInterceptor
     */
    @Autowired
    RedisUrlCountInterceptor redisUrlCountInterceptor;
    /**
     * 定义静态资源行为
     * @param registry
     */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                //所以请求都被拦截包括静态资源
                .addPathPatterns("/**")
                //放行的请求
                .excludePathPatterns("/", "/login", "/css/**", "/fonts/**","/images/**", "/js/**");

        registry.addInterceptor(redisUrlCountInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/css/**", "/fonts/**","/images/**", "/js/**");
    }
}
